function h = jcb_plot_spectra(filename,spectrum,savefigure, fileformat)

if nargin<4
    fileformat='png';
end
if nargin<3
    savefigure=false;
end
if nargin<2
    spectrum='scattering';
end
if nargin<1
    % get user to pick files
    [filelist,pathname] = jcb_pick_files('*spectra.txt', ...
    'Please select files for which to export spectra',true,pwd);
else
    [filelist,pathname] = jcb_select_files(filename,'spectra');
end
if isempty(filelist)
    return
end
%%
if strcmp(spectrum,'scattering')
    spectcol=2;
    ytag='scat';
elseif strcmp(spectrum,'absorption')
    spectcol=3;
    ytag='abs';
elseif strcmp(spectrum,'extinction')
    spectcol=4;
    ytag='ext';
end
if size(filelist,2)>size(filelist(1))
    filelist=filelist';
end

filelist=strtrim(filelist);
n = size(filelist,1);
locstr = cell(n,1);
%%
for ii=1:n
    %% load file & extract data
    filename=filelist{ii};
    disp(['extracting spectra from file ' filename]);
    data=load(fullfile(pathname,filename));
    if ii==1
        lambda=data(:,1);
        spectra = zeros(length(lambda),n);
    end
    
    spectra(:,ii) = data(:,spectcol);
        
    fileinfo=regexp(filename,'dose(?<btl>\d+)_(?<btw>\d+)_(?<gap>\d+)','names');
    mesh=regexp(filename,'m(?<mesh>\d+)','names');
    fileinfo.mesh=str2double(mesh.mesh);
    loc=regexp(filename,'loc(?<loc>\d+)sweep_d(?<d>\d+)','names');
    if isempty(loc)
        loc=regexp(filename,'loc(?<loc>\d+)','names');
    end
    switch str2double(loc.loc)
        case 1
            if length(fields(loc))>1
                locstr{ii}=loc.d;
            else
                locstr{ii}='10';
            end
        case 2
            locstr{ii}='20nm';
        case 3
            locstr{ii}='40nm';
        case 4
            locstr{ii}='108nm';
        case 5
            if length(fields(loc))>1
                locstr{ii}=['corner' loc.d];
            else
                locstr{ii}='corner';
            end
        case 6
            locstr{ii}='no Xtal';  % as no crystal used
    end    
end

h = figure(24578);
plot(lambda,spectra);
legend(locstr);
title([spectrum ' spectra for ' fileinfo.btl 'nm structures using ' ...
       num2str(fileinfo.mesh/100) 'nm mesh']);
xlim([550 850]);
xlabel('\lambda /nm');
ylabel(['\sigma_{' ytag '} /\mum^2']);

if savefigure
    print(['-d' fileformat],fullfile(pathname,['dose_' fileinfo.btl ...
           '_m' num2str(fileinfo.mesh) '_spectra']));
    close(24578);
end

if nargout<1
    clear h
end
end